package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensApplicationUserRelation;
import com.ds.lens.data.service.dao.sql.provider.LensApplicationUserRelationSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LensApplicationUserRelationDao {
    @Delete({
            "delete from lens_application_user_relation",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into lens_application_user_relation (id, application_id, ",
            "user_name, user_type,application_role)",
            "values (#{id,jdbcType=BIGINT}, #{applicationId,jdbcType=BIGINT}, ",
            "#{userName,jdbcType=VARCHAR}, #{userType,jdbcType=TINYINT},#{applicationRole,jdbcType=TINYINT})"
    })
    int insert(LensApplicationUserRelation record);

    @InsertProvider(type = LensApplicationUserRelationSqlProvider.class, method = "insertSelective")
    int insertSelective(LensApplicationUserRelation record);

    @Select({
            "select",
            "id, application_id, user_name, user_type,application_role",
            "from lens_application_user_relation",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "relationMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "application_id", property = "applicationId", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_name", property = "userName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "user_type", property = "userType", jdbcType = JdbcType.TINYINT),
            @Result(column = "application_role", property = "applicationRole", jdbcType = JdbcType.TINYINT)
    })
    LensApplicationUserRelation selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensApplicationUserRelationSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensApplicationUserRelation record);

    @Update({
            "update lens_application_user_relation",
            "set application_id = #{applicationId,jdbcType=BIGINT},",
            "user_name = #{userName,jdbcType=VARCHAR},",
            "user_type = #{userType,jdbcType=TINYINT}",
            "application_role = #{applicationRole,jdbcType=TINYINT}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(LensApplicationUserRelation record);

    @Select({
            "select",
            "user_name",
            "from lens_application_user_relation",
            "where application_id = #{applicationId,jdbcType=BIGINT}"
    })
    List<String> queryUserNamesByApplicationId(Long applicationId);

    @Select({
            "select",
            "count(1) > 0",
            "from lens_application_user_relation",
            "where application_id = #{applicationId,jdbcType=BIGINT} ",
            "and user_name = #{userName,jdbcType=VARCHAR}"
    })
    boolean associated(@Param("applicationId") Long applicationId,
                       @Param("userName") String userName);


    @Select({
            "select",
            "id, application_id, user_name, user_type,application_role",
            "from lens_application_user_relation",
            "where application_id = #{appId,jdbcType=BIGINT}"
    })
    @ResultMap("relationMap")
    List<LensApplicationUserRelation> queryAppUsersByAppId(Long appId);

    @Select({
            "select",
            "count(1) > 0",
            "from lens_application_user_relation",
            "where application_role = 1 and application_id = #{applicationId,jdbcType=BIGINT} ",
            "and user_name = #{userName,jdbcType=VARCHAR}"
    })
    boolean isApplicationAdmin(@Param("applicationId") Long applicationId,
                               @Param("userName") String userName);
}
